package ace.handling;

import gov.nist.atlas.Annotation;
import gov.nist.atlas.Id;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Set;

import org.mitre.jawb.atlas.SubordinateSetsAnnotation;

public class RelationFactory {

	private Hashtable<Id,Relation> relations;
	private Document doc;
	private Set<String> types;
	private Hashtable<String, List<Relation>> typesTable;

	
	public RelationFactory(Document document) {
		this.types = new HashSet<String>();
		this.typesTable = new Hashtable<String, List<Relation>>();
		relations = new Hashtable<Id, Relation>();
		this.doc = document;
		
	}


	public Relation generateRelation(Annotation mention) {
		
		Id id = mention.getId();
		
		Relation r = relations.get(id);
		
		if (r == null){
			
			r = new Relation(id);
			
			relations.put(id,r);

			if (mention.getContent().getContentType().getName().equals("ace_rdc-type-content")){
			
				r.type = mention.getContent().getParameterWithRole("ace_rdc-type").getValueAsString();
			
				addType(r.type);
				
				addRelation(r.type,r);
				
				r.subType = mention.getContent().getParameterWithRole("ace_rdc-subtype").getValueAsString();
			
				addType(r.subType);
				
				addRelation(r.subType,r);
				
			}
			
			r.doc = doc;
			
			
			
		}
		
		return r;

		
	}


	private void addRelation(String type, Relation r) {
		
		List<Relation> list = typesTable.get(type);
		
		if (list == null){
			
			list = new ArrayList<Relation>();
			
			typesTable.put(type,list);
		}
		
		list.add(r);
		
	}


	private void addType(String type) {
		
		types.add(type);
		
	}


	public List<Relation> getRelations(String rel) {
		return typesTable.get(rel);
	}


	public String[] getRelationsList() {
		return types.toArray(new String[0]);
	}


	public Relation generateInferredRelation(String type, Entity e1, Entity e2, String id, int index) {
		
		return new Relation(type,e1,e2,id,index);
		
	}

}
